package com.yanqu.road.server.cmd.activity.operamask;

import com.yanqu.road.entity.activity.operamask.OperaMaskConfig;
import com.yanqu.road.entity.activity.operamask.OperaMaskUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.OperaMaskProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.operamask.OperaMaskModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.operamask.OperaMaskMgr;
import com.yanqu.road.server.pb.OperaMaskPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_OPERA_MASK_CHOOSE_SKILL, desc = "")
public class OperaMaskChooseSkillCmd extends AbstractCommand {

    private int type;
    private int skillId;

    private OperaMaskUserData userData;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OperaMaskProto.OperaMaskChooseSkillReqMsg reqMsg = OperaMaskProto.OperaMaskChooseSkillReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        skillId = reqMsg.getSkillId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        OperaMaskConfig config = OperaMaskMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        userData = player.getModule(OperaMaskModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_OPERA_MASK_NO_UNLOCK;
            return;
        }

        if (type == 1) {
            ret = player.getModule(OperaMaskModule.class).chooseSkill(skillId);
        } else if (type == 2) {
            // ret = player.getModule(OperaMaskModule.class).skipChooseSkill();
        } else if (type == 3) {
            ret = player.getModule(OperaMaskModule.class).randomSkill();
        } else {
            ret = GameErrorCode.E_OPERA_MASK_SKILL_NO_EXIST;
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            OperaMaskProto.OperaMaskStartSceneRespMsg.Builder builder = OperaMaskProto.OperaMaskStartSceneRespMsg.newBuilder();
            builder.setRet(ret);
            if (userData != null) {
                builder.setUserData(OperaMaskPb.parseOperaMaskUserDataMsg(userData));
                if (userData.getGameData() != null) {
                    builder.setGameData(OperaMaskPb.parseOperaMaskGameDataMsg(userData.getGameData(), userData));
                }
            }
            player.sendPacket(ClientProtocol.U_OPERA_MASK_START_SCENE, builder);
        }
    }
}
